<?php
class ModelSaleCashup extends Model {
public function getCashup($id) {
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cashup WHERE id = '" . (int)$id . "'");
    return $query->row;
}
    public function getTotalCashups() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "cashup");

        return $query->row['total'];
    }

    public function addCashup($data) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "cashup SET account = '" . $this->db->escape($data['account']) . "', `status` = 'ACTIVE'");

        $this->cache->delete('cashup');

        return $this->db->getLastId();
    }

    public function editCashup($id, $data) {
        $this->db->query("UPDATE " . DB_PREFIX . "cashup SET status = '" . $this->db->escape($data['status']) . "' WHERE id = '" . (int)$id . "'");

        $this->cache->delete('cashup');
    }

    public function deleteCashup($id) {
//        $this->db->query("DELETE FROM " . DB_PREFIX . "telephone WHERE id = '" . (int)$id . "'");
        $this->db->query("UPDATE " . DB_PREFIX . "cashup SET status = 'DELETE' WHERE id = '" . (int)$id . "'");
        $this->cache->delete('cashup');
    }

    public function getCashups($data = array()) {
        $sql = "SELECT c.*,a.firstname FROM " . DB_PREFIX . "cashup c left join ".DB_PREFIX."affiliate a on a.affiliate_id = c.affiliate_id where c.status <> 'DELETE'";
//        echo $sql;
//        if (!empty($data['filter_name'])) {
//            $sql .= " AND LCASE(ad.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
//        }
//
//        if (!empty($data['filter_attribute_group_id'])) {
//            $sql .= " AND a.attribute_group_id = '" . $this->db->escape($data['filter_attribute_group_id']) . "'";
//        }

        $sort_data = array(
            'id',
            'account',
            'register_time',
            'account_time',
            'firstname',
            'status'
//            'a.sort_order'
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY c.id";
        }

        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 10;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->rows;
    }

}